package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;


@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     *
     * @param dto
     */
    @Override
    public void add(ShoppingCartDTO dto) {

        Long userId = BaseContext.getCurrentId();


        if (dto.getDishId() != null) {


            ShoppingCart shoppingCart = new ShoppingCart();
            DishVO dish = dishMapper.getDish(dto.getDishId());

            shoppingCart.setName(dish.getName());
            shoppingCart.setImage(dish.getImage());
            shoppingCart.setDishId(dto.getDishId());
            shoppingCart.setDishFlavor(dto.getDishFlavor());
            shoppingCart.setAmount(dish.getPrice());
            shoppingCart.setUserId(userId);
            shoppingCart.setCreateTime(LocalDateTime.now());


            //如果能用dishid查到结果，说明里边有相同的菜品，只需要判断口味就可以了
            List<ShoppingCart> cartBy = shoppingCartMapper.getCartBy(shoppingCart);

            if (cartBy != null && !cartBy.isEmpty()) {

                for (ShoppingCart cart : cartBy) {
                    if (Objects.equals(cart.getDishFlavor(), dto.getDishFlavor())) {
                        shoppingCart.setNumber(cart.getNumber() + 1);
                        shoppingCartMapper.updateShoppingCart(shoppingCart);
                        return;
                    }
                }

            }
            shoppingCart.setNumber(1);
            shoppingCartMapper.addShoppingCart(shoppingCart);

        }

        if (dto.getSetmealId() != null) {


            ShoppingCart shoppingCart = new ShoppingCart();
            SetmealVO setmeaVO = setmealMapper.getSetmealById(dto.getSetmealId());

            shoppingCart.setName(setmeaVO.getName());
            shoppingCart.setImage(setmeaVO.getImage());
            shoppingCart.setSetmealId(dto.getSetmealId());
            shoppingCart.setAmount(setmeaVO.getPrice());
            shoppingCart.setUserId(userId);
            shoppingCart.setCreateTime(LocalDateTime.now());

            //如果能用dishid查到结果，说明里边有相同的菜品，只需要判断口味就可以了
            List<ShoppingCart> cartBy = shoppingCartMapper.getCartBy(shoppingCart);

            if (cartBy != null && !cartBy.isEmpty()) {
                shoppingCart.setNumber(cartBy.get(0).getNumber() + 1);
                shoppingCartMapper.updateShoppingCart(shoppingCart);
                return;
            }
            shoppingCart.setNumber(1);
            shoppingCartMapper.addShoppingCart(shoppingCart);

        }
    }

    /**
     * 查看购物车
     *
     * @return
     */
    @Override
    public List list() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        return shoppingCartMapper.getCartBy(shoppingCart);
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCartMapper.clean(shoppingCart);
    }

    /**
     * 减少购物车中的一个
     *
     * @param dto
     */
    @Override
    public void subtuact(ShoppingCartDTO dto) {
        ShoppingCart shoppingCart = new ShoppingCart();
        if (dto.getDishId() != null) {
            shoppingCart.setDishId(dto.getDishId());
            shoppingCart.setUserId(BaseContext.getCurrentId());
            List<ShoppingCart> cartBy = shoppingCartMapper.getCartBy(shoppingCart);
            for (ShoppingCart cart : cartBy) {

                if (Objects.equals(cart.getDishFlavor(), dto.getDishFlavor())) {
                    shoppingCart.setId(cart.getId());
                    if (cart.getNumber() > 1) {

                        shoppingCart.setNumber(cart.getNumber() - 1);
                        shoppingCartMapper.updateShoppingCart(shoppingCart);
                        return;
                    } else {
                        shoppingCartMapper.clean(shoppingCart);
                        return;
                    }

                }
            }
        }
        if (dto.getSetmealId() != null) {
            shoppingCart.setSetmealId(dto.getSetmealId());
            shoppingCart.setUserId(BaseContext.getCurrentId());
            List<ShoppingCart> cartBy = shoppingCartMapper.getCartBy(shoppingCart);
            for (ShoppingCart cart : cartBy) {
                if (cart.getNumber() > 1) {
                    shoppingCart.setId(cart.getId());
                    shoppingCart.setNumber(cart.getNumber() - 1);
                    shoppingCartMapper.updateShoppingCart(shoppingCart);
                } else {
                    shoppingCartMapper.clean(shoppingCart);
                }

            }
        }

    }
}

